export function checkPlatform(platform) {
    let userAgent = navigator.userAgent;

    platform = platform.toUpperCase();

    if (/android/i.test(userAgent)) {
        return platform === `ANDROID` //这是Android平台下浏览器
    }
    if (/(iPhone|iPad|iPod|iOS)/i.test(userAgent)) {
        return platform === `IOS` //这是iOS平台下浏览器
    }
    if (/MicroMessenger/i.test(userAgent)) {
        return platform === `WECHAT ` //这是微信平台下浏览器
    }
}

export function copyProperties(target, source) {
    for (let key of Reflect.ownKeys(source)) {
        if (key !== "constructor" &&
            key !== "prototype" &&
            key !== "name"
        ) {
            let desc = Object.getOwnPropertyDescriptor(source, key);
            Object.defineProperty(target, key, desc);
        }
    }
}


export function checkType(target) {
    let arr = ['Object', 'Number', 'String', 'Null', 'Array', 'Boolean', 'RegExp', 'Date', 'Undefined', 'Error', 'Function']
    let types = {};
    let toString = Object.prototype.toString;
    for (let a of arr) {
        types[`[object ${a}]`] = a;
    }
    let str = toString.call(target);
    return types[str];
}

// 通过配置字符串 `obj.obj` 获取到上一级对象并且赋值给 `obj.obj`这个属性
export function getObjOrSetVal(obj, key, value) {

    let keys = typeof key === 'string' ? key.split('.') : key;
    let len = keys.length;
    let arg = keys.shift();

    if (len == 1) {
        if (notEmpty(value)) {
            obj[arg] = value;
        }
        return obj;
    } else {
        let sub = obj[arg];
        getObjOrSetVal(sub, keys, value);
    }
}

export function notEmpty(value) {
    return typeof value !== 'undefined' && value !== null;
}